package com.bitdf.txing.order.controller;


import cn.dev33.satoken.annotation.SaCheckLogin;
import com.bitdf.txing.common.enume.TxCodeEnume;
import com.bitdf.txing.common.utils.R;
import com.bitdf.txing.order.entity.OrderEntity;
import com.bitdf.txing.order.service.OrderService;
import com.bitdf.txing.order.vo.OrderConfirmVo;
import com.bitdf.txing.order.vo.OrderSubmitVo;
import com.bitdf.txing.order.vo.SubmitOrderResponseVo;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.concurrent.ExecutionException;


/**
 * 订单
 *
 * @author lizhiwei
 * @email sunlightcs@gmail.com
 * @date 2022-05-16 23:47:48
 */
@RestController
@RequestMapping("${txing.request.path.order}")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    RabbitTemplate rabbitTemplate;

    /**
     * 列表
     */
//    @RequestMapping("/list")
//    public R list(@RequestParam Map<String, Object> params){
//        PageUtils page = orderService.queryPage(params);
//
//        return R.ok().put("page", page);
//    }

    /**
     * 获取订单状态
     * @param orderSn
     * @return
     */
    @SaCheckLogin
    @GetMapping("/status/{orderSn}")
    public R getOrderStatus(@PathVariable("orderSn") String orderSn){
        OrderEntity orderEntity=orderService.getOrderByOrderSn(orderSn);
        return R.ok().setData(orderEntity);
    }


    /**
     * 获取确认订单页数据
     * @return
     * @throws ExecutionException
     * @throws InterruptedException
     */
    @GetMapping("/comfirm/get")
    @SaCheckLogin
    public R getComfirmDate() throws ExecutionException, InterruptedException {
        OrderConfirmVo confirmVo = orderService.confirmOrder();
        return R.ok(confirmVo);
    }

    /**
     * 下单(提交订单)
     *
     * @return
     */
    @SaCheckLogin
    @PostMapping("/submit")
    public R submitOrder(@RequestBody OrderSubmitVo vo) {
        System.out.println("");
        try {
            SubmitOrderResponseVo responseVo = orderService.submitOrder(vo);
            //下单成功来到支付选择页
            //下单失败回到订单确认页重新确定订单信息
            if (responseVo.getCode() == 0) {
                //成功
                return R.ok(responseVo);
            } else {
                String msg = "下单失败";
                switch (responseVo.getCode()) {
                    case 1:
                        msg += "令牌订单信息过期，请刷新再次提交";
                        break;
                    case 2:
                        msg += "订单商品价格发生变化，请确认后再次提交";
                        break;
                    case 3:
                        msg += "库存锁定失败，商品库存不足";
                        break;
                }
                return R.error(msg);
            }
        } catch (Exception e) {
            return R.error(TxCodeEnume.WARE_NOT_STOCK_EXCEPTION);
        }
    }


    /**
     * 信息
     */
    @SaCheckLogin
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		OrderEntity order = orderService.getById(id);

        return R.ok().put("order", order);
    }

    /**
     * 修改
     */
    @SaCheckLogin
    @RequestMapping("/update")
    public R update(@RequestBody OrderEntity order){
		orderService.updateById(order);
        return R.ok();
    }

    /**
     * 删除
     */
    @SaCheckLogin
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		orderService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }


}
